package es.aradesh.ministryassist.logic.ministry;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.provider.BaseColumns;

import es.aradesh.ministryassist.db.DbAdapter;
import es.aradesh.ministryassist.db.NoteColumns;

public class NoteBuilder {
	public static long createNote(DbAdapter db, Home home, String text, int type) {
		long id = -1;
		if (!"".equals(text)) {
			id = db.insertNote(home.getId(), text, type);
		}
		return id;
	}

	public static Note getNoteById(DbAdapter db, long id) {
		Note result = null;
		ContentValues val = db.selectById(DbAdapter.TABLE_NOTES, id);
		result = new Note(id, val.getAsString(NoteColumns.DESCRIPTION),
				HomeBuilder.getHomeById(db, val.getAsLong(NoteColumns.HOME_ID)
						.longValue()), val.getAsInteger(NoteColumns.TYPE)
						.intValue(), Date.valueOf(val
						.getAsString(NoteColumns.CREATIONDATE)));
		return result;
	}

	public static List<Note> getNotesByHome(DbAdapter db, Home home) {
		List<Note> result = new ArrayList<Note>();
		Note temp = null;
		String[] cols = new String[] { BaseColumns._ID };
		Cursor c = db.selectTable(DbAdapter.TABLE_NOTES, cols,
				NoteColumns.HOME_ID + " = " + home.getId(), null);
		c.moveToFirst();
		while (!c.isAfterLast()) {
			temp = getNoteById(db, c.getLong(c.getColumnIndex(BaseColumns._ID)));
			c.moveToNext();
			result.add(temp);
		}
		return result;
	}
}
